<?php
/**
 * [WeEngine System] Copyright (c) 2014 WE7.CC
 * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
 */
load()->model('user');
load()->func('tpl');
load()->model('permission');
$_W['token'] = token();
$session = json_decode(authcode($_GPC['__session']), true);
if (is_array($session)) {
	$user = user_single(array('uid'=>$session['uid']));
	if (is_array($user) && $session['hash'] === md5($user['password'] . $user['salt'])) {
		$_W['uid'] = $user['uid'];
		$_W['username'] = $user['username'];
		$user['currentvisit'] = $user['lastvisit'];
		$user['currentip'] = $user['lastip'];
		$user['lastvisit'] = $session['lastvisit'];
		$user['lastip'] = $session['lastip'];
		$_W['user'] = $user;
		$_W['isfounder'] = user_is_founder($_W['uid']);
		unset($founders);
	} else {
		isetcookie('__session', false, -100);
	}
	unset($user);
}
unset($session);

if (!empty($_GPC['__uniacid'])) {
	$_W['uniacid'] = intval($_GPC['__uniacid']);
} else {
	$_W['uniacid'] = uni_account_last_switch();
}
if (!empty($_W['uniacid'])) {
	$_W['uniaccount'] = $_W['account'] = uni_fetch($_W['uniacid']);
	$_W['acid'] = $_W['account']['acid'];
	$_W['weid'] = $_W['uniacid'];
}
if (!empty($_W['uid'])) {
	$_W['highest_role'] = permission_account_user_role($_W['uid']);
	$_W['role'] = permission_account_user_role($_W['uid'], $_W['uniacid']);
}
$_W['template'] = !empty($_W['setting']['basic']['template']) ? $_W['setting']['basic']['template'] : 'default';

$_W['attachurl'] = $_W['attachurl_local'] = $_W['siteroot'] . $_W['config']['upload']['attachdir'] . '/';
if (!empty($_W['setting']['remote'][$_W['uniacid']]['type'])) {
	$remote = $_W['setting']['remote'][$_W['uniacid']];
} else {
	$remote = $_W['setting']['remote'];
}
if (!empty($remote['type'])) {
	if ($remote['type'] == ATTACH_FTP) {
		$_W['attachurl'] = $_W['attachurl_remote'] = $remote['ftp']['url'] . '/';
	} elseif ($remote['type'] == ATTACH_OSS) {
		$_W['attachurl'] = $_W['attachurl_remote'] = $remote['alioss']['url'] . '/';
	} elseif ($remote['type'] == ATTACH_QINIU) {
		$_W['attachurl'] = $_W['attachurl_remote'] = $remote['qiniu']['url'] . '/';
	} elseif ($remote['type'] == ATTACH_COS) {
		$_W['attachurl'] = $_W['attachurl_remote'] = $remote['cos']['url'] . '/';
	}
}
load()->func('compat.biz');